#include "InetAddress.h"
#include <strings.h>
#include <cstring>

InetAddress::InetAddress(uint16_t port, std::string ip)
{
	bzero(&addr_, sizeof addr_);
	addr_.sin_family = AF_INET;
	addr_.sin_port = htons(port);
	addr_.sin_addr.s_addr = inet_addr(ip.c_str());

}

uint16_t InetAddress::toPort() const
{
	return ntohs(addr_.sin_port);
}

std::string InetAddress::toIp() const
{
	char buf[16] = { 0 };
	return inet_ntop(AF_INET, &addr_.sin_addr.s_addr, buf, sizeof buf);
}

std::string InetAddress::toIpPort() const
{
	char buf[24] = { 0 };
	inet_ntop(AF_INET, &addr_.sin_addr.s_addr, buf, sizeof buf);
	size_t end = strlen(buf);
	sprintf(buf + end, ":%u", toPort());
	return buf;
}

#include <iostream>
int main()
{
	InetAddress iadd(8080, "192.168.0.11");
	std::cout << "ip = " << iadd.toIp() << std::endl;
	std::cout << "Ip+Port = " << iadd.toIpPort() << std::endl;
	getchar();
	return 0;
}